﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Qianwei.Model;
using Qianwei.Biz;
using Qianwei.Common.Extension;
using Qianwei.Common;

namespace Qianwei.Web.Admins
{
    public partial class AddJob : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["jid"] != null)
            {
                BindData();
            }
        }

        private void BindData()
        {
            int jid = Request["jid"].TryInt();
            job_info jobinfo = JobInfoManager.Instance.GetModel(jid);
            txtJob_Position.Text = jobinfo.Job_Position;
            txtJob_Number.Text = jobinfo.Job_Number.ToString();
            txtJob_Address.Text = jobinfo.Job_Address;
            txtJob_Treatment.Text = jobinfo.Job_Treatment;
            txtEffective_Duration.Text = jobinfo.Effective_Duration.ToString("yyyy-MM-dd");
            hf_contentinit.Value = jobinfo.Job_Requirement;
        }

        private string CheckInput()
        {
            int jid = Request["jid"].TryInt();
            string strErr = string.Empty;
            if (txtJob_Position.Text.Trim().Length == 0)
            {
                strErr = "请输入职位名称";
            }
            //else if (JobInfoManager.Instance.ExistsJobPosition(txtJob_Position.Text.Trim()))
            //{
            //    strErr = "该职位已存在";
            //}
            else if (txtJob_Number.Text.Trim().Length == 0)
            {
                strErr = "请输入招聘人数";
            }
            else if (!Util.IsInteger(txtJob_Number.Text.Trim()))
            {
                strErr = "请输入正确的招聘人数";
            }
            else if (txtJob_Address.Text.Trim().Length == 0)
            {
                strErr = "请输入工作地点";
            }
            else if (txtJob_Treatment.Text.Trim().Length == 0)
            {
                strErr = "请输入待遇";
            }
            else if (txtEffective_Duration.Text.Trim().Length == 0)
            {
                strErr = "请输入有效期限";
            }
            else if (!System.Text.RegularExpressions.Regex.IsMatch(txtEffective_Duration.Text.Trim(), @"^\d{4}-\d{1,2}-\d{1,2}$"))
            {
                strErr = "请输入正确格式的有效期限";
            }
            //else if (txtJob_Requirement.Text.Trim().Length == 0)
            //{
            //    strErr = "请输入职位要求";
            //}
            else if (hfJob_Requirement.Value.Trim().Length == 0)
            {
                strErr = "请输入职位要求";
            }
            return strErr;
        }

        private void SaveJob()
        {
            int jid = Request["jid"].TryInt();
            job_info jobinfo = new job_info();
            jobinfo.Create_Time = DateTime.Now;
            jobinfo.Effective_Duration = txtEffective_Duration.Text.Trim().TryDateTime();
            jobinfo.Job_Address = txtJob_Address.Text.Trim();
            jobinfo.Job_Id = jid;
            jobinfo.Job_Position = txtJob_Position.Text.Trim();
            jobinfo.Job_Requirement = hfJob_Requirement.Value;
            jobinfo.Job_Treatment = txtJob_Treatment.Text.Trim();
            jobinfo.Last_Change_Time = jobinfo.Create_Time;
            jobinfo.Publish_Time = jobinfo.Create_Time;
            jobinfo.Job_Number = txtJob_Number.Text.Trim().TryInt();

            if (jid == 0)
            {
                if (JobInfoManager.Instance.Add(jobinfo) > 0)
                {
                    //BindData();
                    hf_contentinit.Value = hfJob_Requirement.Value;
                    Util.PageAlert("提交职位信息成功");
                }
                else
                {
                    Util.PageAlert("对不起,提交职位信息失败,请稍后重试");
                }
            }
            else
            {
                if (JobInfoManager.Instance.Update(jobinfo))
                {
                    BindData();
                    Util.PageAlert("更新职位信息成功");
                }
                else
                {
                    Util.PageAlert("对不起,更新职位信息失败,请稍后重试");
                }
            }
        }
        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string result = CheckInput();
            if (result.Length > 0)
            {
                Util.PageAlert(result);
            }
            else
            {
                SaveJob();
            }
        }
        #endregion
    }
}